package cn.db.tools.dbtools.controller;

import cn.db.tools.dbtools.message.ToastMessage;
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.google.common.io.BaseEncoding;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import org.pomo.toasterfx.model.impl.ToastTypes;

import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * @author：zooooooooy
 * @date: 2023/8/9 - 18:10
 */
public class Sha1Controller implements Initializable {

    @FXML
    private TextArea originalTextArea;

    @FXML
    TextField textFieldHex;

    @FXML
    TextField textFieldBase64;

    @FXML
    ChoiceBox choiceBox;

    BaseEncoding baseEncoding = BaseEncoding.base64();

    @FXML
    protected void originTextChanged() {

        originTextChanged(choiceBox.getSelectionModel().selectedIndexProperty().get());
    }

    private void originTextChanged(int type) {

        HashFunction hashFunction = null;
        switch (type) {
            case 0:
                hashFunction = Hashing.sha1();
                break;
            case 1:
                hashFunction = Hashing.sha256();
                break;
            case 2:
                hashFunction = Hashing.sha512();
                break;
        }

        HashCode hashCode = hashFunction.hashBytes(originalTextArea.getText().getBytes(StandardCharsets.UTF_8));
        textFieldHex.setText(hashCode.toString());
        textFieldBase64.setText(baseEncoding.encode(hashCode.asBytes()));
    }



    @FXML
    protected void copyTextFieldHex() {
        Clipboard sc = Clipboard.getSystemClipboard();
        ClipboardContent clipboardContent = new ClipboardContent();
        clipboardContent.put(DataFormat.PLAIN_TEXT, textFieldHex.getText());

        sc.setContent(clipboardContent);

        // 弹出toast消息
        ToastMessage.getInstance().popMessage("复制成功", ToastTypes.SUCCESS);
    }

    @FXML
    protected void copyTextFieldBase64() {
        Clipboard sc = Clipboard.getSystemClipboard();
        ClipboardContent clipboardContent = new ClipboardContent();
        clipboardContent.put(DataFormat.PLAIN_TEXT, textFieldBase64.getText());

        sc.setContent(clipboardContent);

        // 弹出toast消息
        ToastMessage.getInstance().popMessage("复制成功", ToastTypes.SUCCESS);
    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        choiceBox.setItems(FXCollections.observableArrayList("SHA1", "SHA256", "SHA512"));
        choiceBox.getSelectionModel().select(0);

        choiceBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                // 触发改变
                originTextChanged((int) newValue);
            }
        });
    }
}
